// 全排列 II
class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    bool check[9];
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        if (pos == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == false && (i == 0 || nums[i] != nums[i - 1] 
                || check[i - 1] == true))
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums, pos + 1);
                path.pop_back();
                check[i] = false;
            }
        }
    }
};

class Solution {
    string hash[10] = {"", "", "abc", "def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    vector<string> ret;
    string path;
public:
    vector<string> letterCombinations(string digits) {
        if (digits.empty()) return ret;
        dfs(digits, 0);
        return ret;
    }
    void dfs(string digits, int pos)
    {
        if (pos == digits.size())
        {
            ret.push_back(path);
            return;
        }
        for (char e : hash[digits[pos] - '0'])
        {
            path.push_back(e);
            dfs(digits, pos + 1);
            path.pop_back();
        }
    }
};

class Solution {
    int left = 0, right = 0;
    vector<string> ret;
    string path;
public:
    vector<string> generateParenthesis(int n) {
        dfs(n);
        return ret;
    }
    void dfs(int n)
    {
        if (right == n)
        {
            ret.push_back(path);
            return;
        }
        if (left < n)
        {
            path.push_back('('), left++;
            dfs(n);
            path.pop_back(), left--;
        }
        if (right < left)
        {
            path.push_back(')'), right++;
            dfs(n);
            path.pop_back(), right--;
        }
    }
};

class Solution {
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> combine(int n, int k) {
        dfs(n, k, 1);
        return ret;
    }
    void dfs(int n, int k, int pos)
    {
        if (path.size() == k)
        {
            ret.push_back(path);
            return;
        }
        for (int i = pos; i <= n; i++)
        {
            path.push_back(i);
            dfs(n, k, i + 1);
            path.pop_back();
        }
    }
};

class Solution {
    int ret = 0, path = 0;
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        dfs(nums, target, 0, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int target, int pos, int path)
    {
        if (pos == nums.size())
        {
            if (target == path) ret++;
            return;
        }
        dfs(nums, target, pos + 1, path - nums[pos]);
        dfs(nums, target, pos + 1, path + nums[pos]);
    }
};

class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    int aim;
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        aim = target;
        dfs(candidates, 0, 0);
        return ret;
    }
    void dfs(vector<int>& cand, int pos, int sum)
    {
        if (sum == aim)
        {
            ret.push_back(path);
            return;
        }
        if (sum > aim || pos == cand.size()) return;
        for (int i = pos; i < cand.size(); i++)
        {
            path.push_back(cand[i]);
            dfs(cand, i, sum + cand[i]);
            path.pop_back();
        }
    }
};

class Solution {
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        if (pos == nums.size())
        {
            ret.push_back(path);
            return;
        }
        // 选
        path.push_back(nums[pos]);
        dfs(nums, pos + 1);
        path.pop_back();
        // 不选
        dfs(nums, pos + 1);
    }
};
